package com.software.levelupandroid.home.ui.taskByPriority;

import android.content.Context;

import androidx.annotation.NonNull;

import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.utils.JsonUtil;
import com.software.levelupandroid.utils.OkhttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @Description 四象限显示界面的Repository（即MVVM架构中的Model层次）
 * @Author LiangYaJie
 * @CreateTime 2024年06月03日 14:24:10
 */
public class TaskByPriorityRepository {
    private static TaskByPriorityRepository taskByPriorityRepository;
    private List<Task> taskList = new ArrayList<>();

    public static TaskByPriorityRepository getTaskByPriorityRepository(){
        if (taskByPriorityRepository == null){
            taskByPriorityRepository = new TaskByPriorityRepository();
        }
        return taskByPriorityRepository;
    }

    /**
     * 根据优先级获取任务
     * @param callBack
     * @param context
     * @param date
     * @param priority
     */
    public void getTaskByPriority(CallBack<List<Task>> callBack, Context context, String date, int priority){

        Call call = OkhttpUtil.getRequest("task/getTasksByPriority?date=" + date + "&priority=" + priority, context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();
                Result<List<Task>> result = JsonUtil.convertJsonToTaskList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<Task> tasks = result.getData();
                        callBack.onSuccess(tasks);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取任务失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }
            }
        });
    }
}
